package com.mstf.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.mstf.bean.OrderForm;
import com.mstf.bean.User;
import com.mstf.service.OrderFormService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class OrderFormAction extends ActionSupport implements ModelDriven<OrderForm>
{
	private OrderForm orderForm;               // 用于ModelDriven填充对象,入口为getModel方法
	private OrderFormService orderFormService; // 用于spring自动注入，入口为setUserService方法
	
	
	public String creat() throws Exception
	{
		return "creatOrderPage";
	}

	public String InsertInDatabase() throws Exception
	{
	    User user;
		
		HttpServletRequest request = ServletActionContext.getRequest(); // 获取当前request对象
		user = (User) request.getSession().getAttribute("User");        // 将对象装入session
		orderForm.setUserId(user.getUser_id());                         // 获取用户id
		orderForm.setTime(GetCurrentTime());                            // 获取订单时间
		orderForm.setState(0);                                          // 设置状态为未处理
		orderForm.setThreshold(5);                                      // 设置默认阈值
		orderFormService.save(orderForm);                               // 将订单写入数据库
		return "InsertInDatabaseSuccess";
	}

	public String GetCurrentTime()
	{
		 Date date=new Date();
		 SimpleDateFormat simleDateFormat=new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
		 String ret=simleDateFormat.format(date);                    //系统时间
	     return ret;
	}

	@Override
	public OrderForm getModel()
	{
		orderForm = new OrderForm();
		return orderForm;
	}

	public OrderFormService getOrderFormService()
	{
		return orderFormService;
	}

	public void setOrderFormService(OrderFormService orderFormService)
	{
		this.orderFormService = orderFormService;
	}
}
